package com.shsmi.dsn.controller;

import com.shsmi.dsn.common.Result;
import com.shsmi.dsn.domain.DsnExamine;
import com.shsmi.dsn.domain.DsnTask;
import com.shsmi.dsn.service.ExamineService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class ExamineController {
    @Autowired
    private ExamineService  examineService;

    @GetMapping(value = "/examine")
    @ResponseBody
    public Result getExamine(@RequestParam String classify,
                             @RequestParam String examine_time){

        if(classify == null || examine_time==null){
            return Result.genFailResult();
        }
        List<DsnExamine> examineList = examineService.list(classify,examine_time);
        if(examineList.size() == 1){
            examineService.insert(classify, examine_time);
            examineList = examineService.list(classify,examine_time);
            return Result.genSuccessResult(examineList);
        }else{
            return Result.genSuccessResult(examineList);
        }
    }

    @PostMapping(path = "/examine/{id}")
    @ResponseBody
    Result update(@PathVariable long id,
                  DsnExamine record) {
        try {
            DsnExamine tempRecord = examineService.getById(id);
            BeanUtils.copyProperties(record, tempRecord, new String[]{"id"});
            return Result.genSuccessResult(examineService.update(tempRecord));
        } catch (Exception e) {
            return Result.genFailResult();
        }
    }
}
